package org.ipl.process;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ProcessPoints extends HttpServlet { 
	Map resultMap = new TreeMap();
	Map winnerMap = new TreeMap();
	Map momMap = new TreeMap();
	Map sixesMap = new TreeMap();
	Map sixesTeamMap = new TreeMap();
	List mccPlayers = new ArrayList();
	int unusedPoints = 0;
	
	

	/**
	 * @return the resultMap
	 */
	public Map getResultMap() {
		return resultMap;
	}

	/**
	 * @param resultMap the resultMap to set
	 */
	public void setResultMap(Map resultMap) {
		this.resultMap = resultMap;
	}
	
	/**
	 * @return the mccPlayers
	 */
	public List getMccPlayers() {
		return mccPlayers;
	}


	/**
	 * @param mccPlayers the mccPlayers to set
	 */
	public void setMccPlayers(List mccPlayers) {
		this.mccPlayers = mccPlayers;
	}

	String db_driver = getServletConfig().getInitParameter("db_driver");
	String db_user = getServletConfig().getInitParameter("db_user");
	String db_password = getServletConfig().getInitParameter("db_password");
	String db_url = getServletConfig().getInitParameter("db_url");

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String matchId = (String)request.getAttribute("matchId");
		String league = (String)request.getAttribute("league");
		
		
		try {
			Class.forName(db_driver);
			Connection conn = DriverManager.getConnection(db_url, db_user, db_password);
			Statement stmt = conn.createStatement();
			System.out.println("Getting Match Results from DB");
			System.out.print("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");
			ResultSet rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner from matchresults where matchId='"+matchId+"'");

			System.out.println("Adding Match Results from DB to resultMap");
			while (rset.next()) {
				resultMap.put("matchId" , matchId);
				resultMap.put("winner" , rset.getString(1));
				resultMap.put("mom" , rset.getString(2));
				resultMap.put("sixes" , rset.getString(3));
				resultMap.put("sixesWinner" , rset.getString(4));
			}
			System.out.println("resultMap matchId "+resultMap.get("matchId"));
			System.out.println("resultMap winner "+resultMap.get("winner"));
			System.out.println("resultMap mom "+resultMap.get("mom"));
			System.out.println("resultMap sixes "+resultMap.get("sixes"));
			System.out.println("resultMap sixesWinner "+resultMap.get("sixesWinner"));
			
			System.out.println("Getting Answers of Quiz from DB adding to different map's based on parameter(winner, mom, sixes, sixesTeam)");
			rset = stmt.executeQuery("select winner, mom, sixes, sixesWinner, userName, league from answers where matchId='"+matchId+"' and league in('MCC')");

			while (rset.next()) {
				winnerMap.put(rset.getString(5), rset.getString(1));
				momMap.put(rset.getString(5), rset.getString(2));
				sixesMap.put(rset.getString(5), rset.getString(3));
				sixesTeamMap.put(rset.getString(5), rset.getString(4));
			}
			
			Map winnerPointsMap = processWinner(winnerMap);
			Map momPointsMap = processMom(winnerMap);
			Map sixesPointsMap = processSixes(winnerMap, sixesTeamMap);
			
			
			rset.close();
			stmt.close();
			conn.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
		}    
    }
	
	
	private Map processWinner(Map winMap) {
		Map winnerPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("winner")).equals((String) winMap.get(player))){
				loss.add(player);
			}
			else if (((String)resultMap.get("winner")).equals((String) winMap.get(player))) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			winnerPointsMap.put("unusedWinnerPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0;p<gain.size();p++){
				winnerPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+pointsGained);
			}
			
		}else if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0;p<loss.size();p++){
				winnerPointsMap.put(loss.get(p), pointsLost);
				System.out.println(gain.get(p) +":LOSS:"+pointsLost);
			}
		}
		return winnerPointsMap;
	}
	
	private Map processMom(Map momMap) {
		Map momPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("mom")).equals((String) momMap.get(player))){
				loss.add(player);
			}
			else if (((String)resultMap.get("mom")).equals((String) momMap.get(player))) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			momPointsMap.put("unusedMomPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0;p<gain.size();p++){
				momPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+pointsGained);
			}
			
		}else if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0;p<loss.size();p++){
				momPointsMap.put(loss.get(p), pointsLost);
				System.out.println(gain.get(p) +":LOSS:"+pointsLost);
			}
		}
		
		return momPointsMap;
	}
	
	private Map processSixes(Map sixMap, Map sixesTeam) {
		Map sixesPointsMap = new TreeMap();
		List gain = new ArrayList();
		List loss = new ArrayList();
		
		for (int i=0;i<mccPlayers.size();i++) {
			String player = (String) mccPlayers.get(i);
			System.out.println("Player Name: "+player);
			if (!((String)resultMap.get("sixes")).equals((String) sixMap.get(player)) ||
					!((String)resultMap.get("sixesWinner")).equals((String) sixesTeam.get(player)) ){
				loss.add(player);
			}
			else if (((String)resultMap.get("sixes")).equals((String) sixMap.get(player)) &&
					((String)resultMap.get("sixesWinner")).equals((String) sixesTeam.get(player)) ) {
				gain.add(player);
			}
		}
		
		if(gain.size() == 0){
			sixesPointsMap.put("unusedSixesPoints", 80);
		}else if(gain.size() > 0){
			int pointsGained = 80 / gain.size();
			for(int p=0;p<gain.size();p++){
				sixesPointsMap.put(gain.get(p), pointsGained);
				System.out.println(gain.get(p) +":GAIN:"+pointsGained);
			}
			
		}else if(loss.size() > 0){
			int pointsLost = -10;
			for(int p=0;p<loss.size();p++){
				sixesPointsMap.put(loss.get(p), pointsLost);
				System.out.println(gain.get(p) +":LOSS:"+pointsLost);
			}
		}
		
		return sixesPointsMap;
	}
	
	private void populateMCCPlayers() {
		
		try {
			Class.forName(db_driver);
			Connection conn = DriverManager.getConnection(db_url, db_user, db_password);
			Statement stmt = conn.createStatement();
			ResultSet rset = stmt.executeQuery("select userName from users where league='MCC'");

			while (rset.next()) {
				mccPlayers.add(rset.getString(1));
			}
			System.out.println("MccPlayers SIZE "+mccPlayers.size());
			rset.close();
			stmt.close();
			conn.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
		}   
	}

}
